import api from './_api'
import * as Base from './_base'

export var Table = 'User'
export var sendCode = async (values)=>{
    let {phone} = values
    localStorage.clear()
    let codeRes =  await api(`/auth/get_code?token=${await Base.localToken()}`,{phone})
    return codeRes
}
export var login = async (values)=>{
    let {phone,code} = values
    var userRes = await api(`/auth/login?token=${await Base.localToken()}`,{phone,code})
    localStorage.user = JSON.stringify(userRes.data.item)
    return userRes
}
export var logout = async ()=>{
    console.log("models/user logout")
    var userRes = await api(`/auth/logout?token=${await Base.localToken()}`,{})
    localStorage.clear()
    return userRes
}
export var check = async ()=>{
    var userRes = await api(`/auth/account?token=${await Base.localToken()}`,{})
    if(userRes.status == 'success'){
        localStorage.user = JSON.stringify(userRes.data.item)
        return userRes
    }else{
        return {status:'fail'}
    }
}
export var loadCacheUser = ()=>{
  return JSON.parse(localStorage.user||'{}')
}
Base._getThis(exports)
